﻿using System;
using System.Drawing;
using System.IO;
using System.Windows.Forms;

namespace CGM
{
	class TextureCubeDataItem : DataItem
	{
		/////////////////////////////////////////////////////////////////////////////
		// public methods

		public TextureCubeDataItem()
			: base("Cube Texture Files", "texcube")
		{
			//// create and set up the picture boxes
			//m_PictureBoxes = new PictureBox[6];
			//for( int i = 0; i < 6; ++i )
			//{
			//    m_PictureBoxes[i] = new PictureBox();

			//    m_PictureBoxes[i].BackgroundImage = Properties.Resources.Background;
			//    m_PictureBoxes[i].Size = new Size(256, 128);
			//    m_PictureBoxes[i].SizeMode = PictureBoxSizeMode.StretchImage;
			//    m_PictureBoxes[i].Click += new EventHandler(PictureBox_Click);

			//    ControlPanel.Controls.Add(m_PictureBoxes[i]);
			//}

			// create the images
			m_Images = new Bitmap[6][];

			//// event handlers
			//ControlPanel.SizeChanged += new EventHandler(ControlPanel_SizeChanged);
		}

		/////////////////////////////////////////////////////////////////////////////
		// protected override methods

		protected override bool SaveFile(string _strFilename)
		{
			// open the file
			FileStream fsave = new FileStream(_strFilename, FileMode.Create);
			BinaryWriter bsave = new BinaryWriter(fsave);

			// write the width, height, and mip count
			bsave.Write((UInt32)m_Images[0][0].Width);
			bsave.Write((UInt32)m_Images[0][0].Height);
			bsave.Write((UInt32)m_Images[0].Length);

			// write all 6 images
			for (int iImage = 0; iImage < 6; ++iImage)
			{
				// write the image data
				for (int i = 0; i < m_Images[iImage].Length; ++i)
				{
					// write the current mip
					for (int y = 0; y < m_Images[iImage][i].Width; ++y)
					{
						for (int x = 0; x < m_Images[iImage][i].Height; ++x)
						{
							bsave.Write(m_Images[iImage][i].GetPixel(x, y).R);
							bsave.Write(m_Images[iImage][i].GetPixel(x, y).G);
							bsave.Write(m_Images[iImage][i].GetPixel(x, y).B);
						}
					}
				}
			}

			// close the file
			bsave.Close();
			fsave.Close();

			// success
			return true;
		}

		protected override bool OpenFile(string _strFilename)
		{
			// open the file
			FileStream fopen = new FileStream(_strFilename, FileMode.Open);
			BinaryReader bopen = new BinaryReader(fopen);

			// read the width, height, and mip count
			int iWidth = (int)bopen.ReadUInt32();
			int iHeight = (int)bopen.ReadUInt32();
			int iMipCount = (int)bopen.ReadUInt32();

			// load all 6 images
			for (int iImage = 0; iImage < 6; ++iImage)
			{
				// create the current image
				m_Images[iImage] = new Bitmap[iMipCount];

				// read the image data
				int iMipWidth = iWidth;
				int iMipHeight = iHeight;
				for (int i = 0; i < iMipCount; ++i)
				{
					// create the current mip
					m_Images[iImage][i] = new Bitmap((int)iMipWidth, (int)iMipHeight);

					// read the current mip
					for (int y = 0; y < iMipHeight; ++y)
					{
						for (int x = 0; x < iMipWidth; ++x)
						{
							byte R = bopen.ReadByte();
							byte G = bopen.ReadByte();
							byte B = bopen.ReadByte();
							m_Images[iImage][i].SetPixel(x, y, Color.FromArgb(R, G, B));
						}
					}

					// the next mip is half the size
					iMipWidth /= 2;
					iMipHeight /= 2;
				}
			}

			// close the file
			bopen.Close();
			fopen.Close();

			// update the form
			ControlPanelRedraw();

			// success
			return true;
		}

		/////////////////////////////////////////////////////////////////////////////
		// event handlers

		private void PictureBox_Click(object sender, EventArgs e)
		{
			//for (int i = 0; i < 6; ++i)
			//{
			//    if (m_PictureBoxes[i] == sender)
			//    {
			//        m_Images[i] = LoadImage();
			//        break;
			//    }
			//}
			//ControlPanelRedraw();
			//Dirty = true;
		}

		private void ControlPanel_SizeChanged(object sender, EventArgs e)
		{
			ControlPanelResize();
		}

		/////////////////////////////////////////////////////////////////////////////
		// private methods

		private Bitmap[] LoadImage()
		{
			Bitmap[] ret = null;

			OpenFileDialog ofd = new OpenFileDialog();
			if (ofd.ShowDialog() == DialogResult.OK)
			{
				// load the image
				Bitmap image = new Bitmap(ofd.FileName);

				// create mipmaps
				int iWidth = image.Width;
				int iHeight = image.Height;

				uint uMipCount = 1 + (uint)Math.Floor(Math.Log(Math.Max(iWidth, iHeight), 2));
				ret = new Bitmap[uMipCount];

				for (uint i = 0; i < uMipCount; ++i)
				{
					ret[i] = new Bitmap(iWidth, iHeight);
					Graphics g = Graphics.FromImage((Image)ret[i]);
					g.DrawImage(image, 0, 0, iWidth, iHeight);

					// the next mip has half the width and height
					iWidth /= 2;
					iHeight /= 2;
				}
			}

			return ret;
		}

		private void ControlPanelRedraw()
		{
			//for (int i = 0; i < 6; ++i)
			//{
			//    if (m_Images[i] != null)
			//    {
			//        int iWidth = m_Images[i][0].Width * 2;
			//        int iHeight = m_Images[i][0].Height;

			//        m_PictureBoxes[i].Image = new Bitmap(iWidth, iHeight);
			//        Graphics screen = Graphics.FromImage(m_PictureBoxes[i].Image);
			//        int iX = 0;
			//        for (uint j = 0; j < m_Images[i].Length; ++j)
			//        {
			//            screen.DrawImage(m_Images[i][j], iX, 0);
			//            iX += m_Images[i][j].Width;
			//        }
			//    }
			//}
		}

		private void ControlPanelResize()
		{
			//Size size = new Size(768, 512);
			//Point location = new Point();

			//if (size.Width < ControlPanel.Width)
			//{
			//    location.X = (ControlPanel.Width - size.Width) / 2;
			//}
			//else
			//{
			//    location.X = 0;
			//}

			//if (size.Height < ControlPanel.Height)
			//{
			//    location.Y = (ControlPanel.Height - size.Height) / 2;
			//}
			//else
			//{
			//    location.Y = 0;
			//}

			//m_PictureBoxes[0].Location = new Point(512, 128) + ((Size)location);
			//m_PictureBoxes[1].Location = new Point(0, 128) + ((Size)location);
			//m_PictureBoxes[2].Location = new Point(256, 0) + ((Size)location);
			//m_PictureBoxes[3].Location = new Point(256, 256) + ((Size)location);
			//m_PictureBoxes[4].Location = new Point(256, 128) + ((Size)location);
			//m_PictureBoxes[5].Location = new Point(256, 384) + ((Size)location);
		}

		/////////////////////////////////////////////////////////////////////////////
		// private fields

		private Bitmap[][] m_Images;
	}
}
